﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TypingG
{
    public partial class MainGame : Form
    {
        private int level = 0;
        private int style = 0;
        Timer timer1 = new Timer();
        public MainGame()
        {
           InitializeComponent();
        }

        private void onclosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.btnAcc.Text == "ShowAccount")
            {
                this.btnAcc.Text = "HideAccount";
                this.btnAcc.BackColor = Color.Red;
            }
            else
            {
                this.btnAcc.Text = "ShowAccount";
                this.btnAcc.BackColor = Color.Green;
            }
            this.tAccInfo.Visible = !this.tAccInfo.Visible;

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string m = "Are you sure this timeout" + this.comboBox1.Text;
            DialogResult r= MessageBox.Show(m, "", MessageBoxButtons.YesNo);
            if(DialogResult.Yes== r)this.comboBox1.Enabled = false;
        }

        private void btnEasy_Click(object sender, EventArgs e)
        {
            level = 1;
            StringRandom.pickLv(1);
            this.btnEasy.BackColor = System.Drawing.Color.Silver;
            this.btnNormal.BackColor = this.btnDiff.BackColor = System.Drawing.Color.Red;
        }

        private void btnNormal_Click(object sender, EventArgs e)
        {
            level = 2;
            StringRandom.pickLv(2);
            this.btnNormal.BackColor = System.Drawing.Color.Silver;
            this.btnEasy.BackColor = this.btnDiff.BackColor = System.Drawing.Color.Red;
        }

        private void btnDiff_Click(object sender, EventArgs e)
        {
            level = 3;
            StringRandom.pickLv(3);
            this.btnDiff.BackColor = System.Drawing.Color.Silver;
            this.btnNormal.BackColor = this.btnEasy.BackColor = System.Drawing.Color.Red;
        }

        private void btnStyle1_Click(object sender, EventArgs e)
        {
            style = 1;
            this.btnStyle1.BackColor = System.Drawing.Color.Silver;
            this.btnStyle2.BackColor = this.btnStyle3.BackColor = System.Drawing.Color.Red;

        }

        private void btnStyle2_Click(object sender, EventArgs e)
        {
            style = 2;
            this.btnStyle2.BackColor = System.Drawing.Color.Silver;
            this.btnStyle1.BackColor = this.btnStyle3.BackColor = System.Drawing.Color.Red;

        }

        private void btnStyle3_Click(object sender, EventArgs e)
        {
            style = 3;
            this.btnStyle3.BackColor = System.Drawing.Color.Silver;
            this.btnStyle2.BackColor = this.btnStyle1.BackColor = System.Drawing.Color.Red;

        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (this.btnPlay.Text == "OkAllSelection")
            {
                if (this.level != 0 && this.style != 0 && this.comboBox1.Enabled == false)
                {
                    StringRandom.Speed = Convert.ToInt16(this.comboBox1.Text) * 10;
                    int _exp = 50 * level * style * (6000 / (Convert.ToInt16(this.comboBox1.Text)));
                    this.tExp.Text = Convert.ToString(_exp);
                    this.btnPlay.Text = "ClickToPlay";
                }
                else MessageBox.Show("Missing Selection");
            }
            else
            {
                this.btnReset.Enabled = false; //no' se~ dc enable lai khi thoi gian chay het(time5->gO)
                this.btnPlay.Enabled = false;
                this.btnPlay.BackColor = System.Drawing.Color.Yellow;
                //lock all button
                this.btnStyle1.Enabled = this.btnStyle2.Enabled = this.btnStyle3.Enabled = false;
                this.btnEasy.Enabled = this.btnNormal.Enabled = this.btnDiff.Enabled = false;
                AccInfo.WILLHAVEEXP = Convert.ToInt16(this.tExp.Text); //diem kinh nghiem ma nguoi choi se~ co' khi thang'
                this.lTimer.Visible = true;
                timer1 = new Timer();
                timer1.Interval = 1000;
                timer1.Start();
                timer1.Tick += new EventHandler(timer1_Tick);
            }
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            if (this.lTimer.Text == "GO")
            {
                
                timer1.Stop();
                this.lTimer.Visible = false;
                //ta hide cái bảng show thông tin người chơi
                this.btnAcc.Text = "ShowAccount";
                this.tAccInfo.Visible = false;
                //begin:get next function here
                pickGameStyle();
                //end
            }
            else
            {
                int num = Convert.ToInt16(this.lTimer.Text);
                num--;
                if (num == 0) this.lTimer.Text = "GO";
                else this.lTimer.Text = Convert.ToString(num);
            }
        }
        private void pickGameStyle()
        {

            tBox t = new tBoxStyle3(); 
            switch (style)
            {
                case 1:  t = new tBoxStyle3(); break;
                case 2:  t = new tBoxStyle1(); break;
                case 3:  t = new tBoxStyle2(); break;
            }
            this.Controls.Add(t);
            t.getStyle();
            t.Disposed += new EventHandler(t_Disposed);
            
        }

        void t_Disposed(object sender, EventArgs e)
        {
            this.btnReset.Enabled = true;
            this.lLevel.Text = AccInfo.Level;
            this.lExp.Text = AccInfo.EXP;
            this.comboBox1.Text = "";
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.btnPlay.Enabled = true;
            this.btnPlay.BackColor = System.Drawing.Color.Lime;
          
            //unlock all button
            this.btnStyle1.Enabled = this.btnStyle2.Enabled = this.btnStyle3.Enabled = true;
            this.btnEasy.Enabled = this.btnNormal.Enabled = this.btnDiff.Enabled = true;
            this.comboBox1.Enabled = true;
            this.lTimer.Text = "5"; //reset lai thoi gian de load game
            style = 0; //hieu la` chua con kieu gamestyle
            level = 0; //hieu la chua con gamelevel
            this.btnEasy.BackColor = this.btnNormal.BackColor = this.btnDiff.BackColor = System.Drawing.Color.Red;
            this.btnStyle1.BackColor = this.btnStyle2.BackColor = this.btnStyle3.BackColor = System.Drawing.Color.Red;
            this.btnPlay.Text = "OkAllSelection";
            this.tExp.Text = "";
        }

        
        
    }
}
